//
//  SLEmoticonKeyboardView.swift
//  Emotion
//
//  Created by lirenqiang on 16/6/1.
//  Copyright © 2016年 lirenqiang. All rights reserved.
//

import UIKit
import SnapKit



private let toolBarHeight = 37
private let keyboardHeight = 220
private let SLEmoticonKeyboardViewCell = "SLEmoticonKeyboardViewCell"


class SLEmoticonKeyboardView: UIView {

    lazy var packages: [SLEmoticonPackage] = SLEmoticonManager().packages
    
    override init(frame: CGRect) {
        let frame = CGRect(x: 0, y: 0, width: 0, height: keyboardHeight)
        super.init(frame: frame)
        
        setupUI()
        sl_layoutSubview()
        
        dispatch_async(dispatch_get_main_queue()) { 
            let indexPath = NSIndexPath(forItem: 0, inSection: 0)
            self.updatePageControl(indexPath)
        }

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //MARK: - 设置UI界面
    private func setupUI() {
        
        addSubview(toolBar)
        addSubview(collectionView)
        addSubview(pageControl)
        
        
        toolBar.selectedIndex = { (index: Int)->() in
            
            let indexpath = NSIndexPath(forItem: 0, inSection: index)
            
            self.collectionView.scrollToItemAtIndexPath(indexpath, atScrollPosition: .Left, animated: false)
            
        }
        
        self.backgroundColor = UIColor.yellowColor()  
    }
    
    //MARK: - 布局子控件
    private func sl_layoutSubview() {
        
        toolBar.snp_makeConstraints { (make) in
            make.left.bottom.right.equalTo(self)
            make.height.equalTo(toolBarHeight)
        }
        
        collectionView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
        }
        
        pageControl.snp_makeConstraints { (make) in
            make.left.right.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top).offset(-5)
        }
        
    }
    
    //MARK: - 懒加载底部ToolBar
    private lazy var toolBar: SLEmoticonToolBar = SLEmoticonToolBar()
    
    //MARK: - 懒加载collectionView
    private lazy var collectionView: UICollectionView = {
       
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = CGSize(width: UIScreen.mainScreen().bounds.width, height: CGFloat(keyboardHeight-toolBarHeight))
        layout.scrollDirection = .Horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        
        let cv = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        
        //注册cell
        cv.registerClass(SLEmoticonPageCell.self, forCellWithReuseIdentifier: SLEmoticonKeyboardViewCell)
        
        cv.delegate = self
        cv.dataSource = self
        cv.backgroundColor = UIColor.redColor()
        
        cv.pagingEnabled = true
        
        return  cv
        
    }()
    
    //MARK: - 懒加载pageControl
    private lazy var pageControl: UIPageControl = {
        
        let p = UIPageControl()
        
        p.numberOfPages = 6
        p.currentPage = 0
        
        p.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "_pageImage")
        p.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "_currentPageImage")
        
        return p 
        
    }()
}

// MARK: - 数据源和代理方法
extension SLEmoticonKeyboardView: UICollectionViewDataSource, UICollectionViewDelegate {
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return packages.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return packages[section].sectionEmoticon.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(SLEmoticonKeyboardViewCell, forIndexPath: indexPath) as! SLEmoticonPageCell
        
        cell.emoticons = packages[indexPath.section].sectionEmoticon[indexPath.item]
        
        return cell
    }
    
//    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
//        let indexPath = collectionView.indexPathsForVisibleItems().last!
//        
//        toolBar.setSelectedButton(indexPath.section)
//    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        var center = collectionView.center
        
        center.x = scrollView.contentOffset.x + center.x
        
        let indexPath = collectionView.indexPathsForVisibleItems()
        
        for index in indexPath {
            
            let cell = collectionView.cellForItemAtIndexPath(index)
            
            if cell!.frame.contains(center) {
                //跟新pageControl的数据
                updatePageControl(index)
                toolBar.setSelectedButton(index.section)
            }
            
        }
    }
}


extension SLEmoticonKeyboardView {
    private func updatePageControl(indexPath: NSIndexPath) {
        
        pageControl.currentPage = indexPath.item
        pageControl.numberOfPages = packages[indexPath.section].sectionEmoticon.count
        
    }
}





































